package com.scrawler.html.template;



import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.htmlparser.NodeFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.scrawler.main.InitProject;
import com.scrawler.regex.template.CrawlArticleContent;
import com.scrawler.tool.UrlFilter;
import com.scrawler.util.HttpUtils;
import com.scrawler.vo.NewsContentConfig;
import com.scrawler.vo.WebConfig;

/**  
 * @Description: 描述
 * @author kuntang    
 * @version 1.0  
 * @created 2012-10-23 上午11:11:21 
 * Copyright (c) 2012 sohu,Inc. All Rights Reserved.
 */

//@Service()
// 非单例
//@Scope("prototype")
public class HtmlPageTemplate implements Runnable{

	private HttpUtils httpUtils = new HttpUtils();
	
	/** 超链接过滤器  */
	public static NodeClassFilter linkFilter = new NodeClassFilter(LinkTag.class);
	
	/** url 正则匹配表达式*/
	public static final String URL_REGEXP_RULES = "^http://.*";
	
	private WebConfig config;
	private String url;
	
	public void init(WebConfig config,String url){
		this.config = config;
		this.url = url;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub	
		
		String content = httpUtils.getContentByUrl(url,config.getEnconding());
		
		System.out.println("length........."+url);
		//System.out.println("content:......>>>>"+content);
		NodeFilter filters[] = null;
//		if(config.getFilter().size()>0){// 
//			filters = createFilters(config.getFilter());
//		}else{// tag name filter
//			filters = createFiltersByTagName(config.getTagName());
//		}
		
		CopyOnWriteArrayList<String> links = parseUrl(content,filters);
		
		// 过滤掉无用的链接,video,pic等.
		StringBuilder sb = new StringBuilder("give up invalid url: ").append("\r\n");
		for(String urlStr : links){
			if(!UrlFilter.matchUrl(urlStr)){
				links.remove(urlStr);
				sb.append(urlStr).append("\r\n");
			}
		}
		
		
		if(config.getWeb_type() == 1){
			System.out.println("==========>type="+config.getWeb_type());
			for(String link : links){
				NewsContentConfig newsConfig = NBAPageTemplate.newsContentConfig.get(config.getId());
				if(newsConfig == null)continue;
				CrawlArticleContent stask = new CrawlArticleContent();
				stask.init(link, newsConfig);
				SecondContentCrawlerPool.getInstance().addTask(stask);
				System.out.println("add link============>"+link);
			}
		}
		
		if(config.getWeb_type() == 2){
			System.out.println("==========>type="+config.getWeb_type());
			for(String link : links){
				NewsContentConfig newsConfig = NBAPageTemplate.newsContentConfig.get(config.getId());
				if(newsConfig == null)continue;
				//CrawlArticleContent stask = InitProject.getContext().getBean(CrawlArticleContent.class);
				//stask.init(link, newsConfig);
				//SecondContentCrawlerPool.getInstance().addTask(stask);
				System.out.println("add link============>"+link);
			}
		}
		
		
		
		
		
		
	}
	
	private CopyOnWriteArrayList<String> parseUrl(String content,NodeFilter filters[]){
		if(filters == null || filters.length<1){
			//LOG.info("filters length<1,return");
			return new CopyOnWriteArrayList();
		}
		
		CopyOnWriteArrayList<String> links = new CopyOnWriteArrayList<String>();
		/** 匹配正文html代码的过滤器*/
		OrFilter headerOrfooterFilter = new OrFilter(filters);
		
//		System.out.println("content:.....>>>>"+content);
		
		
		NodeList divs = null;
		int size = 0;
		StringBuilder sb = new StringBuilder();
		
		return links;
	}

	
	public NodeFilter[] createFilters(List<String[]> names){
		NodeFilter filters[] = new NodeFilter[names.size()];
		for(int i=0;i<names.size();i++){
			String values[] = names.get(i);
			filters[i] = new HasAttributeFilter(values[0],values[1]);
		}
		return filters;
	}
	
	public NodeFilter[] createFilters(String value){
		NodeFilter[] filters = new NodeFilter[1];
		if(value == null)return null;
		String keyValue[] = value.split("=");
		if(keyValue == null || keyValue.length<2)return null;
		filters[0] = new HasAttributeFilter(keyValue[0],keyValue[1]);
		return filters;
	}
	
	public NodeFilter[] createFiltersByTagName(String value){
		
		if(value == null)return null;
		String keyValue[] = value.split(",");
		if(keyValue == null)return null;
		NodeFilter[] filters = new NodeFilter[keyValue.length];
		for(int i=0;i<filters.length;i++){
			filters[i] = new TagNameFilter(keyValue[i]);
		}
		return filters;
	}
	
	
	
	
}
